package com.gexiaobao.pigeon.ui.adapter

import androidx.appcompat.widget.AppCompatImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.RecentContactsResponse
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.databinding.ItemRecentsContactsBinding
import org.json.JSONObject

/**
 * @Author      : hxw
 * @Date        : 2024/2/25 19:03
 * @Describe    :
 */
class RecentContactsAdapter(data: ArrayList<RecentContactsResponse>) :
    BaseQuickAdapter<RecentContactsResponse, DataBindBaseViewHolder>(R.layout.item_recents_contacts, data) {
    override fun convert(holder: DataBindBaseViewHolder, item: RecentContactsResponse) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemRecentsContactsBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        if (item.recents.isNotEmpty()) {
            val jsonObject = JSONObject(Util.decodeBase64(item.recents[0].payload))
            val type = jsonObject.getInt("type")
            val content = if (type == 1) {
                jsonObject.getString("content")
            } else {
                context.getString(R.string.photo_shop_chat)
            }
            holder.setText(R.id.tvMsgContent, content)
        }
        val ivAvatar = holder.getView<AppCompatImageView>(R.id.ivRecentContactHead)
        val userAvatar = item.userAvatar
        if (userAvatar.isEmpty() && item.recents.isNotEmpty()) {
            ImageLoaderManager.loadCircleImageFall(
                context,
                "https://api.multiavatar.com/${item.recents[0].from_uid}.png",
                ivAvatar,
                R.mipmap.head_default_icon
            )
        } else {
            ImageLoaderManager.loadCircleImageFall(context, userAvatar, ivAvatar, R.mipmap.head_default_icon)
        }
    }
}